﻿Imports System.Windows.Forms
Imports System.Drawing


Namespace ControlLibrary
    Public Class dbiWSFDayView


#Region "**Classes**"
        ''' <summary>Event args for use with the public events.</summary>
        Public Class DayViewArgs
            Inherits System.EventArgs

            Private l_argWSFObject As dbiWSF.FrameworkObjects.objWSFObjects
            Private l_argWarehouse As dbiWSF.FrameworkObjects.objWarehouse
            Private l_argLoadingDock As dbiWSF.FrameworkObjects.objLoadingDock
            Private l_argShipment As dbiWSF.FrameworkObjects.objShipment

            Private l_argDbiAppointment As Dbi.PIM.dbiAppointmentItem
            Private l_argDbColumnType As Dbi.WinControl.DayView.dbiColumnType
            Private l_Allow As Boolean = True

            ''' <summary>The MainWSFObject in use.</summary>
            Public Property argWSFObject() As dbiWSF.FrameworkObjects.objWSFObjects
                Get
                    Return l_argWSFObject
                End Get
                Set(ByVal value As dbiWSF.FrameworkObjects.objWSFObjects)
                    l_argWSFObject = value
                End Set
            End Property

            ''' <summary>The warehouse being affected.</summary>
            Public Property argWarehouse() As dbiWSF.FrameworkObjects.objWarehouse
                Get
                    Return l_argWarehouse
                End Get
                Set(ByVal value As dbiWSF.FrameworkObjects.objWarehouse)
                    l_argWarehouse = value
                End Set
            End Property

            ''' <summary>The loading dock being affected.</summary>
            Public Property argLoadingDock() As dbiWSF.FrameworkObjects.objLoadingDock
                Get
                    Return l_argLoadingDock
                End Get
                Set(ByVal value As dbiWSF.FrameworkObjects.objLoadingDock)
                    l_argLoadingDock = value
                End Set
            End Property

            ''' <summary>The shipment being affected.</summary>
            Public Property argShipment() As dbiWSF.FrameworkObjects.objShipment
                Get
                    Return l_argShipment
                End Get
                Set(ByVal value As dbiWSF.FrameworkObjects.objShipment)
                    l_argShipment = value
                End Set
            End Property

            ''' <summary>The dbi.PIM.AppointmentItem being affected.</summary>
            Public Property argDbiAppointment() As Dbi.PIM.dbiAppointmentItem
                Get
                    Return l_argDbiAppointment
                End Get
                Set(ByVal value As Dbi.PIM.dbiAppointmentItem)
                    l_argDbiAppointment = value
                End Set
            End Property

            ''' <summary>The DayView column type of the dbiAppointment being affected.</summary>
            Public Property argDbColumnType() As Dbi.WinControl.DayView.dbiColumnType
                Get
                    Return l_argDbColumnType
                End Get
                Set(ByVal value As Dbi.WinControl.DayView.dbiColumnType)
                    l_argDbColumnType = value
                End Set
            End Property

            ''' <summary>A boolean to indicate if the change is allowed.</summary>
            Public Property Allow() As Boolean
                Get
                    Return l_Allow
                End Get
                Set(ByVal value As Boolean)
                    l_Allow = value
                End Set
            End Property

        End Class

        ''' <summary>Event args for use with public events that need an original shipment timeframe.</summary>
        ''' <remarks>Inherits and extends the DayViewArgs Class</remarks>
        Public Class DayViewArgsShipmentChange
            Inherits DayViewArgs
            Private l_argShipmentLoadingDockTransferTo As dbiWSF.FrameworkObjects.objLoadingDock
            Private l_argIsShipmentLoadingDockTransfer As Boolean
            Private l_argDbiColumnTypeTo As Dbi.WinControl.DayView.dbiColumnType
            Private l_argsOldTimeStart As DateTime
            Private l_argsOldTimeEnd As DateTime
            Private l_argsNewTimeStart As DateTime
            Private l_argsNewTimeEnd As DateTime
            Private l_argsDetailOld As String
            Private l_argsDetailNew As String

            ''' <summary>The loading dock a shipment has been moved to.</summary>
            Public Property argShipmentLoadingDockTransferTo() As dbiWSF.FrameworkObjects.objLoadingDock
                Get
                    Return l_argShipmentLoadingDockTransferTo
                End Get
                Set(ByVal value As dbiWSF.FrameworkObjects.objLoadingDock)
                    l_argShipmentLoadingDockTransferTo = value
                End Set
            End Property

            ''' <summary>A boolean value indicating if the shipment was actually moved to another loadingdock.</summary>
            Public Property argIsShipmentLoadingDockTransfer() As Boolean
                Get
                    Return l_argIsShipmentLoadingDockTransfer
                End Get
                Set(ByVal value As Boolean)
                    l_argIsShipmentLoadingDockTransfer = value
                End Set
            End Property

            ''' <summary>The original TimeStart of the appointment/shipment.</summary>
            Public Property argsOldTimeStart() As DateTime
                Get
                    Return l_argsOldTimeStart
                End Get
                Set(ByVal value As DateTime)
                    l_argsOldTimeStart = value
                End Set
            End Property

            ''' <summary>The original TimeEnd of the appointment/shipment.</summary>
            Public Property argsOldTimeEnd() As DateTime
                Get
                    Return l_argsOldTimeEnd
                End Get
                Set(ByVal value As DateTime)
                    l_argsOldTimeEnd = value
                End Set
            End Property

            ''' <summary>The new TimeStart of the appointment/shipment.</summary>
            Public Property argsNewTimeStart() As DateTime
                Get
                    Return l_argsNewTimeStart
                End Get
                Set(ByVal value As DateTime)
                    l_argsNewTimeStart = value
                End Set
            End Property

            ''' <summary>The new TimeEnd of the appointment/shipment.</summary>
            Public Property argsNewTimeEnd() As DateTime
                Get
                    Return l_argsNewTimeEnd
                End Get
                Set(ByVal value As DateTime)
                    l_argsNewTimeEnd = value
                End Set
            End Property

            ''' <summary>The original shipment description.</summary>
            Public Property argsDetailOld() As String
                Get
                    Return l_argsDetailOld
                End Get
                Set(ByVal value As String)
                    l_argsDetailOld = value
                End Set
            End Property

            ''' <summary>The new shipment description.</summary>
            Public Property argsDetailNew() As String
                Get
                    Return l_argsDetailNew
                End Get
                Set(ByVal value As String)
                    l_argsDetailNew = value
                End Set
            End Property
        End Class
#End Region

#Region "**Public Enums**"
        ''' <summary>An enum value that determines how the DayView is being displayed</summary>
        Public Enum ViewType
            Warehouse = 0
            Loadingdock = 1
        End Enum
#End Region

#Region "**Public Events**"
        Public Event clShipmentBeforeAdd(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgs)
        Public Event clShipmentBeforeChange(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgsShipmentChange)
        Public Event clShipmentBeforeRemove(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgs)

        Public Event clShipmentAfterAdd(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgs)
        Public Event clShipmentAfterChange(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgsShipmentChange)
        Public Event clShipmentAfterRemove(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgs)

        Public Event clShipmentDoubleClick(ByVal sender As Object, ByVal e As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgs)
#End Region

#Region "**Global Variables**"

        Public WithEvents dbiDayViewContextMenu As New dbiWSF.ControlLibrary.dbiWSFObjectMenu()

        Private g_BusinessRulesDialog As dbiWSF.Dialogs.dlgBusinessRule
        Private g_EventArg As Object
        Private g_EventArgMethod As dbiWSF.Dialogs.EventMethods
#End Region

#Region "**Local Variables**"
        Private l_MainWSFObjects As dbiWSF.FrameworkObjects.objWSFObjects
        Private l_MainBusinessRules As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects
        Private l_ColWarehouses As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Private l_ColCargoTypes As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objCargoType)
        Private l_LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock
        Private l_LoadingDockDisplayDates As New System.Collections.Generic.List(Of Date)
        Private l_DayviewViewType As ViewType
        Private l_BusinessRulesOnShipmentAdd As Boolean = True
        Private l_BusinessRulesOnShipmentChange As Boolean = True
        Private l_BusinessRulesOnShipmentRemove As Boolean = True
        Private l_AppointmentMoved As Boolean = False
        Private l_ExceptionColor As Color = Color.IndianRed
        Private l_RecurrenceDailyColor As Color = Color.Tan
        Private l_RecurrenceWeeklyColor As Color = Color.Tan
        Private l_RecurrenceMonthlyColor As Color = Color.Tan
        Private l_RecurrenceCustomColor As Color = Color.Tan
        Private l_TipsOnAppointmentOver As Boolean = True
#End Region

#Region "**Public Properties**"
        ''' <summary>
        ''' The main dbiWSFObjects to use with the dbiWSFDayView control.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF"), _
        System.ComponentModel.Description("The main dbiWSFObjects to use with the dbiWSFDayView control.")> _
        Public Property MainWSFObjects() As dbiWSF.FrameworkObjects.objWSFObjects
            Get
                Return l_MainWSFObjects
            End Get
            Set(ByVal value As dbiWSF.FrameworkObjects.objWSFObjects)
                l_MainWSFObjects = value
                Me.dbiDayViewContextMenu.WSFObjects = value
            End Set
        End Property

        ''' <summary>
        ''' A reference to the main BusinessRules object in the project
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Objects"), _
        System.ComponentModel.Description("A reference to the main BusinessRules object in the project"), _
        System.ComponentModel.Browsable(False)> _
        Public Property MainBusinessRules() As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects
            Get
                Return l_MainBusinessRules
            End Get
            Set(ByVal value As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects)
                l_MainBusinessRules = value
                Me.dbiDayViewContextMenu.MyBusinessRules = value
                Me.g_BusinessRulesDialog = New dbiWSF.Dialogs.dlgBusinessRule(value)
            End Set
        End Property

        ''' <summary>
        ''' Get the dbiDayView Type (multi day or by warehouse).
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("Get the dbiDayView Type (multi day or by warehouse).")> _
        Public ReadOnly Property DayViewViewType() As ViewType
            Get
                Return l_DayviewViewType
            End Get
        End Property

        ''' <summary>
        ''' The start time of the dbiDayView control.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The start time of the dbiDayView control.")> _
        Public Property TimeStart() As Integer
            Get
                Return DbiDayView1.TimeStart
            End Get
            Set(ByVal value As Integer)
                DbiDayView1.TimeStart = value
            End Set
        End Property

        ''' <summary>
        ''' The end time of the dbiDayView control.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The end time of the dbiDayView control.")> _
        Public Property TimeEnd() As Integer
            Get
                Return DbiDayView1.TimeEnd
            End Get
            Set(ByVal value As Integer)
                DbiDayView1.TimeEnd = value
            End Set
        End Property

        ''' <summary>
        ''' The text in the title label of the dbiWSFDayView.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The text in the title label of the dbiWSFDayView.")> _
        Public Property TitleText() As String
            Get
                Return Me.DbiLabel1.Text
            End Get
            Set(ByVal value As String)
                Me.DbiLabel1.Text = value
            End Set
        End Property

        ''' <summary>
        ''' Determines if the dbiWSFDayView displays a print button.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("Determines if the dbiWSFDayView displays a print button.")> _
        Public Property DisplayPrintButton() As Boolean
            Get
                Return ButtonPrint.Visible
            End Get
            Set(ByVal value As Boolean)
                ButtonPrint.Visible = value
            End Set
        End Property

        ''' <summary>
        ''' Determines if the dbiWSFDayView displays a printpreview button.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("Determines if the dbiWSFDayView displays a printpreview button.")> _
        Public Property DisplayPrintPreviewButton() As Boolean
            Get
                Return ButtonPrintPreview.Visible
            End Get
            Set(ByVal value As Boolean)
                ButtonPrintPreview.Visible = value
            End Set
        End Property

        ''' <summary>
        ''' Use BusinessRules when adding a shipment.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Business Rules"), _
        System.ComponentModel.Description("Use BusinessRules when adding a shipment.")> _
        Public Property BusinessRulesOnShipmentAdd() As Boolean
            Get
                Return l_BusinessRulesOnShipmentAdd
            End Get
            Set(ByVal value As Boolean)
                l_BusinessRulesOnShipmentAdd = value
            End Set
        End Property

        ''' <summary>
        ''' Use BusinessRules when changing a shipment.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Business Rules"), _
        System.ComponentModel.Description("Use BusinessRules when changing a shipment.")> _
        Public Property BusinessRulesOnShipmentChange() As Boolean
            Get
                Return l_BusinessRulesOnShipmentChange
            End Get
            Set(ByVal value As Boolean)
                l_BusinessRulesOnShipmentChange = value
            End Set
        End Property

        ''' <summary>
        ''' Use BusinessRules when removing a shipment.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Business Rules"), _
        System.ComponentModel.Description("Use BusinessRules when removing a shipment.")> _
        Public Property BusinessRulesOnShipmentRemove() As Boolean
            Get
                Return l_BusinessRulesOnShipmentRemove
            End Get
            Set(ByVal value As Boolean)
                l_BusinessRulesOnShipmentRemove = value
            End Set
        End Property

        ''' <summary>
        ''' Display dbiTips when over an appointment in the dbiDayView.
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Tips"), _
        System.ComponentModel.Description("Display dbiTips when over an appointment in the dbiDayView.")> _
        Public Property TipsOnAppointmentOver() As Boolean
            Get
                Return l_TipsOnAppointmentOver
            End Get
            Set(ByVal value As Boolean)
                l_TipsOnAppointmentOver = value
            End Set
        End Property

        ''' <summary>
        ''' The primary color to use for dbiWSF Exceptions
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The primary color to use for dbiWSF Exceptions")> _
        Public Property ExceptionColor() As Color
            Get
                Return l_ExceptionColor
            End Get
            Set(ByVal value As Color)
                l_ExceptionColor = value
            End Set
        End Property

        ''' <summary>
        ''' The primary color to use for dbiWSF Exception Recurrences - Daily
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The primary color to use for dbiWSF Exception Recurrences - Daily")> _
        Public Property RecurrenceDailyColor() As Color
            Get
                Return l_RecurrenceDailyColor
            End Get
            Set(ByVal value As Color)
                l_RecurrenceDailyColor = value
            End Set
        End Property

        ''' <summary>
        ''' The primary color to use for dbiWSF Exception Recurrences - Weekly
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The primary color to use for dbiWSF Exception Recurrences - Weekly")> _
        Public Property RecurrenceWeeklyColor() As Color
            Get
                Return l_RecurrenceWeeklyColor
            End Get
            Set(ByVal value As Color)
                l_RecurrenceWeeklyColor = value
            End Set
        End Property

        ''' <summary>
        ''' The primary color to use for dbiWSF Exception Recurrences - Monthly
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The primary color to use for dbiWSF Exception Recurrences - Monthly")> _
        Public Property RecurrenceMonthlyColor() As Color
            Get
                Return l_RecurrenceMonthlyColor
            End Get
            Set(ByVal value As Color)
                l_RecurrenceMonthlyColor = value
            End Set
        End Property

        ''' <summary>
        ''' The primary color to use for dbiWSF Exception Recurrences - Custom
        ''' </summary>
        <System.ComponentModel.Category("dbiWSF Day View"), _
        System.ComponentModel.Description("The primary color to use for dbiWSF Exception Recurrences - Custom")> _
        Public Property RecurrenceCustomColor() As Color
            Get
                Return l_RecurrenceCustomColor
            End Get
            Set(ByVal value As Color)
                l_RecurrenceCustomColor = value
            End Set
        End Property

#End Region

#Region "**dbiWSFDayView Control Methods**"
        ''' <summary>A public method to set the main business rules object.</summary>
        Public Sub SetMainBusinessRules(ByRef objBusinessRules As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects)
            If objBusinessRules IsNot Nothing Then
                MainBusinessRules = objBusinessRules
            End If
        End Sub

        ''' <summary>Displays the dbiWSFDayView using a collection of Warehouses for the column info.</summary>
        Public Sub SetWarehouseDetails(ByRef objWSFObject As dbiWSF.FrameworkObjects.objWSFObjects, _
                                       ByVal Warehouses As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse), _
                                       ByVal DisplayDate As Date, _
                                       Optional ByRef CargoTypes As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objCargoType) = Nothing, _
                                       Optional ByRef objBusinessRules As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects = Nothing)
            Me.DbiDate1.SelectedBackColor = Color.FromArgb(255, 251, 200, 79)
            Me.DbiDate1.Date = DisplayDate
            Me.DbiDate1.ShowDate = DisplayDate
            l_DayviewViewType = ViewType.Warehouse
            If CargoTypes IsNot Nothing Then
                l_ColCargoTypes = CargoTypes
            End If

            Me.DbiDayView1.Date = DisplayDate
            Me.DbiDayView1.DateEnd = DisplayDate

            If Warehouses IsNot Nothing And objWSFObject IsNot Nothing Then
                l_ColWarehouses = Warehouses
                MainWSFObjects = objWSFObject
                LoadDataWarehouses()
            End If
            If objBusinessRules IsNot Nothing Then
                MainBusinessRules = objBusinessRules
            End If
        End Sub
        ''' <summary>Overloaded: Calls the SetWarehouseDetails using a single warehouse object.</summary>
        Public Sub SetWarehouseDetails(ByRef objWSFObject As dbiWSF.FrameworkObjects.objWSFObjects, _
                                       ByVal Warehouse As dbiWSF.FrameworkObjects.objWarehouse, _
                                       ByVal DisplayDate As Date, _
                                       Optional ByRef CargoTypes As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objCargoType) = Nothing, _
                                       Optional ByRef objBusinessRules As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects = Nothing)
            Dim collWhse As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
            collWhse.Add(Warehouse)
            SetWarehouseDetails(objWSFObject, collWhse, DisplayDate, CargoTypes, objBusinessRules)
        End Sub
        ''' <summary>Supporting Method:Fills the dbiDayView from the MainWSFObjects for the warehouses in the SetWarehouseDetail methods.</summary>
        Private Sub LoadDataWarehouses()
            Windows.Forms.Cursor.Current = Cursors.WaitCursor
            Me.DbiDayView1.Enabled = False
            DbiDayViewClearAll()


            If l_ColWarehouses IsNot Nothing Then
                Me.DbiDayView1.AppointmentType = Dbi.WinControl.DayView.enumAppointmentType.Date
                Me.DbiDayView1.GroupType = Dbi.WinControl.DayView.enumGroupType.Location


                For Each warehouse As dbiWSF.FrameworkObjects.objWarehouse In l_ColWarehouses
                    For Each loadingdock As dbiWSF.FrameworkObjects.objLoadingDock In warehouse.LoadingDocks

                        Dim CargoInDock As Boolean = True

                        If l_ColCargoTypes.Count > 0 Then
                            For Each CargoType As dbiWSF.FrameworkObjects.objCargoType In l_ColCargoTypes
                                If loadingdock.CargoTypes.Contains(CargoType.CargoID) Then
                                    CargoInDock = True
                                Else
                                    CargoInDock = False

                                End If
                            Next
                        End If
                        If CargoInDock Then
                            Dim LoadingDockLocation As Dbi.PIM.dbiLocationItem

                            'This IF statement simply sets the header text for each column in the control.
                            ' Can be changed to however the developer wishes to label the columns
                            If l_ColWarehouses.Count > 1 Then
                                LoadingDockLocation = New Dbi.PIM.dbiLocationItem(warehouse.Description + vbCrLf + loadingdock.Description, loadingdock.LoadingDockID.ToString)
                            Else
                                LoadingDockLocation = New Dbi.PIM.dbiLocationItem(loadingdock.Description, loadingdock.LoadingDockID.ToString)
                            End If

                            LoadingDockLocation.Tag = loadingdock
                            Me.DbiDayView1.Locations.Add(LoadingDockLocation)

                            Dim ColShipments = From Shipments As dbiWSF.FrameworkObjects.objShipment In loadingdock.Shipments _
                                          Where Shipments.SchedStartTime.Date <= Me.DbiDayView1.Date And Shipments.SchedEndTime >= Me.DbiDayView1.Date _
                                          Select Shipments

                            For Each Shipment As dbiWSF.FrameworkObjects.objShipment In ColShipments
                                Dim ShipmentAppointment As New Dbi.PIM.dbiAppointmentItem(Shipment.SchedStartTime, Shipment.SchedEndTime)
                                If Shipment.ShipmentType = FrameworkObjects.ShipmentTypes.Inbound Then
                                    ShipmentAppointment.Text = "Inbound: " + Shipment.Description
                                Else
                                    ShipmentAppointment.Text = "Outbound: " + Shipment.Description
                                End If
                                ShipmentAppointment.Tag = Shipment
                                ShipmentAppointment.LocationID = Shipment.LoadingDockID.ToString
                                ShipmentAppointment.ReadOnly = True
                                If ShipmentAppointment.Start.Date <> ShipmentAppointment.End.Date Then
                                    ShipmentAppointment.AllDayEvent = True
                                    ShipmentAppointment.Locked = True
                                End If
                                Me.DbiDayView1.Appointments.Add(ShipmentAppointment)
                            Next

                            For Each GenericException As dbiWSF.FrameworkObjects.objGenericException In MainWSFObjects.GetGenericExceptionsByLoadingDock(loadingdock, Me.DbiDayView1.Date)
                                Dim newCustomArea As New Dbi.PIM.dbiCustomArea

                                newCustomArea.DeadZone = False 'Change to True for automatic canceling
                                newCustomArea.Start = GenericException.DateStart
                                newCustomArea.End = GenericException.DateEnd
                                newCustomArea.LocationID = loadingdock.LoadingDockID.ToString

                                'Parse the exception/recurrence
                                If GenericException.objLoadingDockException IsNot Nothing Then
                                    newCustomArea.BackColor = Me.ExceptionColor
                                    newCustomArea.Tag = GenericException.objLoadingDockException
                                Else
                                    newCustomArea.Tag = GenericException.objRecurrence
                                    'Set customarea color based on recurrence type
                                    Select Case GenericException.objRecurrence.RecurrenceType
                                        Case FrameworkObjects.RecurrenceTypes.Daily
                                            newCustomArea.BackColor = Me.RecurrenceDailyColor
                                        Case FrameworkObjects.RecurrenceTypes.Weekly
                                            newCustomArea.BackColor = Me.RecurrenceWeeklyColor
                                        Case FrameworkObjects.RecurrenceTypes.Monthly
                                            newCustomArea.BackColor = Me.RecurrenceMonthlyColor
                                        Case FrameworkObjects.RecurrenceTypes.Custom
                                            newCustomArea.BackColor = Me.RecurrenceCustomColor
                                        Case Else
                                            newCustomArea.BackColor = Me.RecurrenceDailyColor
                                    End Select
                                End If
                                Me.DbiDayView1.CustomAreas.Add(newCustomArea)
                            Next
                        End If
                    Next
                Next

                If Me.DbiDayView1.Locations.Count > 0 Then
                Else
                    l_ColCargoTypes.Clear()
                    LoadDataWarehouses()
                End If
            End If
            Me.DbiDayView1.Enabled = True
            Windows.Forms.Cursor.Current = Cursors.Default
            Me.DbiDayView1.Refresh()

        End Sub

        ''' <summary>Displays the dbiWSFDayView using a date collection for a specific loading dock.</summary>
        Public Sub SetLoadingDockDetails(ByRef objWSFObject As dbiWSF.FrameworkObjects.objWSFObjects, ByVal LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock, ByVal Dates As System.Collections.Generic.List(Of Date), Optional ByRef objBusinessRules As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects = Nothing)
            l_DayviewViewType = ViewType.Loadingdock
            If Dates IsNot Nothing Then
                Me.l_LoadingDockDisplayDates = Dates
                Me.DbiDate1.Date = Me.l_LoadingDockDisplayDates(0)
                Me.DbiDate1.ShowDate = Me.DbiDate1.Date
                Me.DbiDayView1.Date = Me.DbiDate1.Date
            End If

            If LoadingDock IsNot Nothing And objWSFObject IsNot Nothing Then
                l_LoadingDock = LoadingDock
                MainWSFObjects = objWSFObject

                LoadDataLoadingDocks()
            End If

            If objBusinessRules IsNot Nothing Then
                MainBusinessRules = objBusinessRules
            End If
            SetDayFormats()
        End Sub
        ''' <summary>Overloaded: Calls the SetLoadingDockDetails using a date range.</summary>
        Public Sub SetLoadingDockDetails(ByRef objWSFObject As dbiWSF.FrameworkObjects.objWSFObjects, ByVal LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock, ByVal StartDate As Date, ByVal EndDate As Date, Optional ByRef objBusinessRules As dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects = Nothing)
            SetLoadingDockDetails(objWSFObject, LoadingDock, GetDateRange(StartDate, EndDate), objBusinessRules)
        End Sub
        ''' <summary>Supporting Method:Fills the dbiDayView from the MainWSFObjects for the loading docks in the SetLoadingDockDetails methods.</summary>
        Private Sub LoadDataLoadingDocks()
            Windows.Forms.Cursor.Current = Cursors.WaitCursor
            Me.DbiDayView1.Enabled = False

            DbiDayViewClearAll()

            If l_LoadingDock IsNot Nothing Then

                Me.DbiDayView1.AppointmentType = Dbi.WinControl.DayView.enumAppointmentType.Date
                Me.DbiDayView1.GroupType = Dbi.WinControl.DayView.enumGroupType.None

                Dim tempDates As New System.Collections.Generic.List(Of Date)

                For Each curDate As Date In l_LoadingDockDisplayDates
                    tempDates.Add(curDate)
                Next

                tempDates.Sort()

                If l_LoadingDockDisplayDates IsNot Nothing Then
                    For Each curDate As Date In tempDates
                        Dim newColumn As New Dbi.WinControl.DayView.dbiColumnType(curDate)
                        newColumn.LocationID = l_LoadingDock.LoadingDockID.ToString
                        Me.DbiDayView1.ColumnTypes.Add(newColumn)
                    Next
                End If


                For Each Shipment As dbiWSF.FrameworkObjects.objShipment In l_LoadingDock.Shipments
                    Dim ShipmentAppointment As New Dbi.PIM.dbiAppointmentItem(Shipment.SchedStartTime, Shipment.SchedEndTime, Shipment.Description)
                    ShipmentAppointment.Tag = Shipment
                    ShipmentAppointment.LocationID = Shipment.LoadingDockID.ToString
                    ShipmentAppointment.ReadOnly = True
                    If ShipmentAppointment.Start.Date <> ShipmentAppointment.End.Date Then
                        ShipmentAppointment.AllDayEvent = True
                        ShipmentAppointment.Locked = True
                    End If
                    Me.DbiDayView1.Appointments.Add(ShipmentAppointment)
                Next

                For Each LoadingDockException As dbiWSF.FrameworkObjects.objLoadingDockException In MainWSFObjects.LoadingDockExceptions
                    If l_LoadingDockDisplayDates.Contains(LoadingDockException.StartDate.Date) Or l_LoadingDockDisplayDates.Contains(LoadingDockException.EndDate) Then
                        If LoadingDockException.LoadingDocks.Contains(l_LoadingDock.LoadingDockID) Then
                            Dim DockExceptionArea As New Dbi.PIM.dbiCustomArea(LoadingDockException.StartDate, LoadingDockException.EndDate, ExceptionColor, True)
                            DockExceptionArea.LocationID = l_LoadingDock.LoadingDockID.ToString
                            DockExceptionArea.Tag = LoadingDockException
                            DockExceptionArea.DeadZone = False
                            Me.DbiDayView1.CustomAreas.Add(DockExceptionArea)
                        End If
                    End If
                Next

                For Each Recurrence As dbiWSF.FrameworkObjects.objRecurrence In MainWSFObjects.Recurrences
                    If Recurrence.ObjectType = dbiWSF.FrameworkObjects.RecurrenceObjectType.DockException Then
                        If Recurrence.RecurringObjects.Contains(l_LoadingDock.LoadingDockID) Then
                            Select Case Recurrence.RecurrenceType
                                Case dbiWSF.FrameworkObjects.RecurrenceTypes.Daily
                                    Dim spanDays As Long = DateDiff(DateInterval.Day, Recurrence.RecurringStartDate, Recurrence.RecurringEndDate)
                                    For curDay As Long = 0 To spanDays Step Recurrence.Frequency
                                        Dim nowDate As Date = Recurrence.RecurringStartDate.AddDays(curDay)
                                        Dim newCustomArea As New Dbi.PIM.dbiCustomArea
                                        newCustomArea.BackColor = RecurrenceDailyColor
                                        newCustomArea.DeadZone = False
                                        newCustomArea.Start = nowDate.Date.AddMilliseconds(Recurrence.ObjectStartDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.End = nowDate.Date.AddMilliseconds(Recurrence.ObjectEndDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.Tag = Recurrence
                                        AddCustomArea(newCustomArea)
                                    Next
                                Case dbiWSF.FrameworkObjects.RecurrenceTypes.Monthly
                                    Dim spanMonth As Long = DateDiff(DateInterval.Month, Recurrence.RecurringStartDate, Recurrence.RecurringEndDate)

                                    For curMonth As Long = 0 To spanMonth Step Recurrence.Frequency
                                        Dim nowDate As Date = Recurrence.RecurringStartDate.AddMonths(curMonth)
                                        Dim newCustomArea As New Dbi.PIM.dbiCustomArea
                                        newCustomArea.BackColor = RecurrenceMonthlyColor
                                        newCustomArea.DeadZone = False
                                        newCustomArea.Start = nowDate.Date.AddMilliseconds(Recurrence.ObjectStartDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.End = nowDate.Date.AddMilliseconds(Recurrence.ObjectEndDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.Tag = Recurrence
                                        AddCustomArea(newCustomArea)
                                    Next
                                Case dbiWSF.FrameworkObjects.RecurrenceTypes.Weekly
                                    Dim myDates As New System.Collections.Generic.List(Of Date)
                                    myDates = MainWSFObjects.FixedDates(Recurrence.RecurringStartDate, Recurrence.RecurringEndDate, Recurrence.Frequency, Recurrence.DaysOfWeek)
                                    For Each curDate As Date In myDates
                                        Dim newCustomArea As New Dbi.PIM.dbiCustomArea
                                        newCustomArea.BackColor = RecurrenceWeeklyColor
                                        newCustomArea.DeadZone = False
                                        newCustomArea.Start = curDate.Date.AddMilliseconds(Recurrence.ObjectStartDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.End = curDate.Date.AddMilliseconds(Recurrence.ObjectEndDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.Tag = Recurrence
                                        AddCustomArea(newCustomArea)
                                    Next

                                Case dbiWSF.FrameworkObjects.RecurrenceTypes.Custom

                                    Dim myDates As New System.Collections.Generic.List(Of Date)
                                    'Pass in date collection here.
                                    'myDates = DateCollection
                                    For Each curDate As Date In myDates
                                        Dim newCustomArea As New Dbi.PIM.dbiCustomArea
                                        newCustomArea.BackColor = RecurrenceCustomColor
                                        newCustomArea.DeadZone = False
                                        newCustomArea.Start = curDate.Date.AddMilliseconds(Recurrence.ObjectStartDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.End = curDate.Date.AddMilliseconds(Recurrence.ObjectEndDate.TimeOfDay.TotalMilliseconds)
                                        newCustomArea.Tag = Recurrence
                                        AddCustomArea(newCustomArea)
                                    Next

                            End Select

                        End If
                    End If
                Next


            End If
            Me.DbiDayView1.Enabled = True
            Windows.Forms.Cursor.Current = Cursors.Default
            Me.DbiDayView1.Refresh()
        End Sub

        ''' <summary>Refreshes the dbiDayView based on the MainWSFObject</summary>
        ''' <remarks>If the databinding on the MaindbiWSFObjects is set to automatic then you can refresh
        ''' the dayview from other forms by calling this method. If the data is unbound then refresh the MainWSFObjects
        ''' first before calling this method.</remarks>
        Public Sub DataRefresh()
            Select Case l_DayviewViewType
                Case ViewType.Loadingdock
                    LoadDataLoadingDocks()
                Case ViewType.Warehouse
                    LoadDataWarehouses()
            End Select
        End Sub

        ''' <summary>Returns a dbi.PIM.dbiAppointmentItem given a ShipmentID.</summary>
        ''' <param name="ShipmentID">The dbiWSF.Shipment Id GUID to find.</param>
        ''' <remarks>If the shipmentID isn't currently shown in the dbiDayView then this method will return nothing.</remarks>
        Public Function GetAppointmentByShipmentID(ByVal ShipmentID As System.Guid) As Dbi.PIM.dbiAppointmentItem
            Dim curAppt As Dbi.PIM.dbiAppointmentItem

            Dim linqAppt = From dbiAppt As Dbi.PIM.dbiAppointmentItem In Me.DbiDayView1.Appointments _
                           Where dbiAppt.Tag.ShipmentID = ShipmentID _
                           Select dbiAppt

            If linqAppt.Count > 0 Then
                curAppt = linqAppt(0)
            Else
                curAppt = Nothing
            End If

            Return curAppt
        End Function

        ''' <summary>Ensures that a specific appointment is visible in the dbiDayView.</summary>
        ''' <param name="ShipmentID">The dbiWSF.Shipment Id GUID to find.</param>
        ''' <remarks>If the shipmentID isn't added to the current view of the dbiDayView then the control will not scroll.</remarks>
        Public Sub ScrollToShipment(ByVal ShipmentID As System.Guid)
            Dim curAppt As Dbi.PIM.dbiAppointmentItem = GetAppointmentByShipmentID(ShipmentID)

            If curAppt IsNot Nothing Then
                Me.DbiDayView1.ShowTime(curAppt.Start.TimeOfDay.TotalMinutes)
                If Me.DayViewViewType = ViewType.Warehouse Then
                    Dim curCol As Integer
                    For Each locItem As Dbi.PIM.dbiLocationItem In Me.DbiDayView1.Locations
                        If locItem.EntryID = curAppt.Tag.LoadingDockID.ToString Then
                            curCol = Me.DbiDayView1.Locations.IndexOf(locItem)
                        End If
                    Next
                    Me.DbiDayView1.HorzScrollValue = curCol
                Else
                    Dim curCol As Integer
                    For Each colType As Dbi.WinControl.DayView.dbiColumnType In Me.DbiDayView1.ColumnTypes
                        If colType.Date = curAppt.Start.Date Then
                            curCol = Me.DbiDayView1.ColumnTypes.IndexOf(colType)
                        End If
                    Next
                    Me.DbiDayView1.HorzScrollValue = curCol
                End If
            End If

        End Sub

        ''' <summary>Supporting Method:Clears all the data from the dbiDayView control.</summary>
        Private Sub DbiDayViewClearAll()
            Me.DbiDayView1.Appointments.Clear()
            Me.DbiDayView1.ColumnTypes.Clear()
            Me.DbiDayView1.Contacts.Clear()
            Me.DbiDayView1.CustomAreas.Clear()
            Me.DbiDayView1.Locations.Clear()
            Me.DbiDayView1.Tasks.Clear()
            Me.DbiDayView1.TitleText = ""
        End Sub

        ''' <summary>Supporting Method: Used to ensure only custom areas that will be used are loaded into dbiDayView.</summary>
        Private Sub AddCustomArea(ByRef CustomAreaToAdd As Dbi.PIM.dbiCustomArea)
            If Me.DayViewViewType = ViewType.Warehouse Then
                If CustomAreaToAdd.Start.Date = Me.DbiDayView1.Date.Date Or CustomAreaToAdd.End.Date = Me.DbiDayView1.Date Then
                    Me.DbiDayView1.CustomAreas.Add(CustomAreaToAdd)
                End If
            Else
                'Type is LoadingDock and use the l_Dates collection
                If l_LoadingDockDisplayDates.Contains(CustomAreaToAdd.Start.Date) Or l_LoadingDockDisplayDates.Contains(CustomAreaToAdd.End.Date) Then
                    Me.DbiDayView1.CustomAreas.Add(CustomAreaToAdd)
                End If
            End If
        End Sub

        ''' <summary>Function to return a collection of dates, based on a supplied start and end.</summary>
        ''' <param name="StartDate"></param>
        ''' <param name="EndDate"></param>
        ''' <returns>a collection of dates, from a specified start to end</returns>
        Private Function GetDateRange(ByVal StartDate As Date, ByVal EndDate As Date) As System.Collections.Generic.List(Of Date)
            Dim DateSpan As New System.Collections.Generic.List(Of Date)
            Dim DateCount As Integer
            Dim CalcStartDate As Date
            Dim CalcEndDate As Date

            If StartDate > EndDate Then
                CalcEndDate = StartDate
                CalcStartDate = EndDate
            Else
                CalcEndDate = EndDate
                CalcStartDate = StartDate
            End If

            For DateCount = 0 To DateDiff(DateInterval.Day, CalcStartDate, CalcEndDate)
                DateSpan.Add(CalcStartDate.AddDays(DateCount))
            Next



            Return DateSpan
        End Function

        ''' <summary>Supporting Method:Used for alpha-channel back fills.</summary>
        Private Sub SetDayFormats()
            Me.DbiDate1.DayFormats.Clear()
            For Each curDate As Date In l_LoadingDockDisplayDates
                Dim newFormat As New Dbi.PIM.dbiDayFormat(curDate, curDate)
                newFormat.BackColor = System.Drawing.Color.FromArgb(255, 251, 200, 79)
                Me.DbiDate1.DayFormats.Add(newFormat)
            Next
        End Sub

        ''' <summary>Supporting Method:Creates a dbiTipsObject for use with the dbiTips when hovering over a dbiAppointment.</summary>
        Private Function MakeTipForAppointment(ByRef curAppointment As Dbi.PIM.dbiAppointmentItem) As Dbi.WinControl.Tips.dbiTipsObject
            'Declare a new dbiTipsObject
            Dim newTip As New Dbi.WinControl.Tips.dbiTipsObject

            'Set the default properties of the dbiTipsObject
            newTip.FillType = Dbi.enumFillType.Vertical
            newTip.HeaderVisible = True
            newTip.FooterVisible = True
            newTip.HeaderImage = My.Resources.TimeBarForTip 'Pull an image from the resources file for the header.
            newTip.HeaderFont = New Font("Trebuchet MS", 12, FontStyle.Bold, GraphicsUnit.Pixel)
            newTip.HeaderText = curAppointment.Text
            newTip.HeaderForeColor = Color.DarkBlue

            Dim footerImage As Image

            Dim curShipment As dbiWSF.FrameworkObjects.objShipment = curAppointment.Tag

            If curShipment.BreakoutDockQty <= 0 Then
                'If there are no breakout docks needed then tip is blue
                newTip.BackColor = Color.White
                'Make a blue fade effect for non-breakout dock tips.
                newTip.BackColorTo = Color.FromArgb(228, 239, 239)
            Else
                'If there are breakout docks then tip is red
                newTip.BackColor = Color.White
                'Make a red fade effect for breakout dock tips.
                newTip.BackColorTo = Color.FromArgb(255, 209, 208)
            End If


            If curShipment.GetCargoTypes.Count > 0 Then

                footerImage = MakeImageFromCargoTypes(curShipment.GetCargoTypes)
                newTip.FooterImage = footerImage
                newTip.FooterText = "Cargo Types in Shipment"
                newTip.FooterFont = New Font("Trebuchet MS", 12, FontStyle.Italic + FontStyle.Bold, GraphicsUnit.Pixel)
                newTip.FooterForeColor = Color.DarkBlue
                Me.DbiTipsMain.FooterLine = True
            Else
                newTip.FooterVisible = False
                Me.DbiTipsMain.FooterLine = False
            End If

            Dim bodyString As String
            bodyString = "Start: " + curAppointment.Start.ToString _
            + vbCrLf + "End: " + curAppointment.End.ToString + vbCrLf

            If curShipment.BreakoutDockQty > 0 Then
                bodyString += vbCrLf + "**Breakout docks required: " + curShipment.BreakoutDockQty.ToString + vbCrLf
            End If

            bodyString += vbCrLf

            If curShipment.OrderHeaders.Count > 0 Then
                bodyString += "Orders on this shipment:" + vbCrLf
            Else
                bodyString += "There are no orders on this shipment." + vbCrLf
            End If

            For Each curOrderHeader As dbiWSF.FrameworkObjects.objOrderHeader In curShipment.OrderHeaders
                bodyString += curOrderHeader.OrderEntryType + ": " + curOrderHeader.OrderEntryID + vbCrLf
            Next

            newTip.BodyFont = New Font("Trebuchet MS", 12, FontStyle.Regular, GraphicsUnit.Pixel)
            newTip.BodyText = bodyString
            newTip.BodyForeColor = Color.Black

            Return newTip

        End Function

        ''' <summary>Supporting Method:Creates an image from the cargo types collection passed to the method.</summary>
        Private Function MakeImageFromCargoTypes(Optional ByVal CargoTypes As System.Collections.Generic.List(Of System.Guid) = Nothing) As Image
            Dim returnImage As Image
            Try
                If CargoTypes IsNot Nothing Then
                    Dim myBitmap As New Bitmap(18 * CargoTypes.Count, 16)
                    Dim myGraphics As Graphics = Graphics.FromImage(myBitmap)
                    Dim curPosition As Long = 0
                    CargoTypes.Sort()

                    For Each CargoType As System.Guid In CargoTypes
                        Dim curCargo As dbiWSF.FrameworkObjects.objCargoType = MainWSFObjects.GetCargoFromGUID(CargoType)
                        Dim curByte() As Byte = curCargo.Icon
                        Dim curImage As Image = Image.FromStream(New System.IO.MemoryStream(curByte))
                        myGraphics.DrawImage(curImage, curPosition, 0)
                        curPosition += 18
                    Next
                    returnImage = myBitmap
                Else
                    returnImage = New Bitmap(16, 16)
                End If
            Catch ex As Exception
                returnImage = New Bitmap(16, 16)
            End Try
            Return returnImage
        End Function
#End Region

#Region "**Base Controls Event Methods**"
        Private Sub DbiDate1_DateClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles DbiDate1.DateClick
            If l_DayviewViewType = ViewType.Warehouse Then
                Me.DbiDate1.Refresh()
                TimerSynchDate.Enabled = True
            End If

        End Sub

        Private Sub DbiDate1_DateSelected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles DbiDate1.DateSelected
            If l_DayviewViewType = ViewType.Loadingdock Then

                'Check to see if the Shift or Ctrl modifier key is being pressed
                Select Case ModifierKeys
                    Case Keys.Control
                        Me.DbiDate1.Date = e.End
                        If l_LoadingDockDisplayDates.Contains(e.Start) Then
                            l_LoadingDockDisplayDates.Remove(e.Start)
                        Else
                            l_LoadingDockDisplayDates.Add(e.Start)
                        End If
                    Case Keys.Shift
                        Dim LastDate As Date = Me.l_LoadingDockDisplayDates.Item(Me.l_LoadingDockDisplayDates.Count - 1)
                        l_LoadingDockDisplayDates.Clear()
                        l_LoadingDockDisplayDates = GetDateRange(LastDate, e.Start)
                    Case Else
                        l_LoadingDockDisplayDates.Clear()
                        l_LoadingDockDisplayDates.Add(e.Start)
                        Me.DbiDate1.SelectedBackColor = Color.FromArgb(255, 251, 200, 79)
                End Select



                TimerSynchDate.Enabled = True
            End If

        End Sub

        Private Sub DbiDayView1_AfterAppointmentAdd(ByVal sender As Object, ByVal e As Dbi.WinControl.DayView.AfterAppointmentAddEventArgs) Handles DbiDayView1.AfterAppointmentAdd
            If e.Appointment.Tag Is Nothing Then
                Dim ShipmentArgs As New DayViewArgs

                ShipmentArgs.argWSFObject = Me.MainWSFObjects

                ShipmentArgs.argDbiAppointment = e.Appointment

                Dim newShipment As New dbiWSF.FrameworkObjects.objShipment(Me.MainWSFObjects)
                newShipment.ShipmentID = System.Guid.NewGuid
                newShipment.SchedStartTime = e.Appointment.Start
                newShipment.SchedEndTime = e.Appointment.End
                newShipment.LoadingDockID = New System.Guid(e.Appointment.LocationID)
                newShipment.Description = ""

                ShipmentArgs.argShipment = newShipment

                ShipmentArgs.argDbiAppointment.Tag = newShipment

                ShipmentArgs.argLoadingDock = MainWSFObjects.GetLoadingDockFromGUID(e.Appointment.Tag.LoadingDockID)

                ShipmentArgs.argWarehouse = Me.MainWSFObjects.GetWarehouseFromGUID(ShipmentArgs.argLoadingDock.WarehouseID)


                RaiseEvent clShipmentBeforeAdd(Me, ShipmentArgs)


                If ShipmentArgs.Allow Then
                    ShipmentArgs.argShipment.AddNew(ShipmentArgs.argLoadingDock)
                    If Me.BusinessRulesOnShipmentAdd Then
                        g_EventArg = ShipmentArgs
                        g_EventArgMethod = Dialogs.EventMethods.DbiDayView1_AfterAppointmentAdd
                        'Fire Timer To Validate the business rules
                        Me.timerEventSynch.Enabled = True
                    Else
                        RaiseEvent clShipmentAfterAdd(Me, ShipmentArgs)
                    End If
                Else
                    Me.DbiDayView1.Appointments.Remove(ShipmentArgs.argDbiAppointment)
                End If


                'If BusinessRulesOnShipmentAdd Then
                '    Dim newBRDialog As New dbiWSF.ControlLibrary.dlgBusinessRule(ShipmentArgs.argShipment, BusinessRulesAndSupport.BusinessRuleObjects.RequestAction.Add, MainBusinessRules, "Business Rule Violation(s)... " & vbCrLf & "Do you want to override and allow the creation of the shipment?", Me.ParentForm)
                '    ShipmentArgs.Allow = newBRDialog.Allow
                '    newBRDialog.Dispose()
                'End If

                'If ShipmentArgs.Allow = True Then
                '    ShipmentArgs.argShipment.AddNew(ShipmentArgs.argLoadingDock)
                '    ShipmentArgs.argDbiAppointment.Text = ShipmentArgs.argShipment.Description
                '    If ShipmentArgs.argShipment.ShipmentType = FrameworkObjects.ShipmentTypes.Outbound Then
                '        ShipmentArgs.argDbiAppointment.BackColorTo = Color.LightGray
                '    End If
                'Else
                '    Me.DbiDayView1.Appointments.Remove(ShipmentArgs.argDbiAppointment)
                'End If

            End If
        End Sub

        Private Sub DbiDayView1_AfterAppointmentChange(ByVal sender As Object, ByVal e As Dbi.WinControl.DayView.AfterAppointmentChangeEventArgs) Handles DbiDayView1.AfterAppointmentChange
            If l_AppointmentMoved Then
                If Not e.Appointment.Locked Then
                    Dim ShipmentMoveArgs As New DayViewArgsShipmentChange

                    If e.Appointment.Tag IsNot Nothing Then
                        If e.Appointment.LocationID.ToString <> e.Appointment.Tag.LoadingDockID.ToString Then
                            'Transfer loading dock
                            ShipmentMoveArgs.argIsShipmentLoadingDockTransfer = True
                            ShipmentMoveArgs.argShipmentLoadingDockTransferTo = MainWSFObjects.GetLoadingDockFromGUID(New System.Guid(e.Appointment.LocationID))
                        Else
                            ShipmentMoveArgs.argIsShipmentLoadingDockTransfer = False
                        End If
                    End If

                    With ShipmentMoveArgs
                        .argDbiAppointment = e.Appointment
                        .argLoadingDock = MainWSFObjects.GetLoadingDockFromGUID(e.Appointment.Tag.LoadingDockID)
                        .argShipment = e.Appointment.Tag
                        .argWarehouse = MainWSFObjects.GetWarehouseFromGUID(.argLoadingDock.WarehouseID)
                        .argWSFObject = MainWSFObjects
                        .argsOldTimeStart = e.Appointment.Tag.SchedStartTime
                        .argsOldTimeEnd = e.Appointment.Tag.SchedEndTime
                        .argsNewTimeStart = e.Appointment.Start
                        .argsNewTimeEnd = e.Appointment.End
                        .argsDetailNew = e.Appointment.Text
                        .argsDetailOld = e.Appointment.Tag.Description
                    End With

                    'a simple validation routine to ensure correct appointment values when moving an appointment from spanning multiple days to a single day.
                    If e.Appointment.End < e.Appointment.Start Then
                        e.Appointment.Start = e.Appointment.End
                        e.Appointment.End = e.Appointment.Start.AddSeconds(DateDiff(DateInterval.Second, e.Appointment.Tag.SchedStartTime, e.Appointment.Tag.SchedEndTime))
                    End If

                    With ShipmentMoveArgs.argShipment
                        .SchedStartTime = e.Appointment.Start
                        .SchedEndTime = e.Appointment.End
                        If ShipmentMoveArgs.argIsShipmentLoadingDockTransfer Then
                            .LoadingDockID = ShipmentMoveArgs.argShipmentLoadingDockTransferTo.LoadingDockID
                        End If
                    End With

                    RaiseEvent clShipmentBeforeChange(Me, ShipmentMoveArgs)

                    If ShipmentMoveArgs.Allow Then
                        If Me.BusinessRulesOnShipmentChange Then
                            g_EventArg = ShipmentMoveArgs
                            g_EventArgMethod = Dialogs.EventMethods.DbiDayView1_BeforeAppointmentChange
                            Me.timerEventSynch.Enabled = True
                        Else
                            RaiseEvent clShipmentAfterChange(Me, ShipmentMoveArgs)
                        End If
                    Else
                        ShipmentMoveArgs.argShipment.SchedStartTime = ShipmentMoveArgs.argsOldTimeStart
                        ShipmentMoveArgs.argShipment.SchedEndTime = ShipmentMoveArgs.argsOldTimeEnd
                        ShipmentMoveArgs.argDbiAppointment.Start = ShipmentMoveArgs.argsOldTimeStart
                        ShipmentMoveArgs.argDbiAppointment.End = ShipmentMoveArgs.argsOldTimeEnd

                        ShipmentMoveArgs.argDbiAppointment.LocationID = ShipmentMoveArgs.argLoadingDock.LoadingDockID.ToString
                        ShipmentMoveArgs.argShipment.LoadingDockID = ShipmentMoveArgs.argLoadingDock.LoadingDockID
                    End If
                End If
                l_AppointmentMoved = False
            End If
        End Sub

        Private Sub DbiDayView1_AppointmentDoubleClick(ByVal sender As Object, ByVal e As Dbi.WinControl.DayView.AppointmentDoubleClickEventArgs) Handles DbiDayView1.AppointmentDoubleClick
            Me.DbiTipsMain.Hide()
            Dim ShipmentArgs As New DayViewArgs
            With ShipmentArgs
                .argWSFObject = MainWSFObjects
                .argDbiAppointment = e.Appointment
                .argShipment = e.Appointment.Tag
                .argLoadingDock = MainWSFObjects.GetLoadingDockFromGUID(e.Appointment.Tag.LoadingDockID)
                .argWarehouse = .argWSFObject.GetWarehouseFromGUID(.argLoadingDock.WarehouseID)
            End With
            RaiseEvent clShipmentDoubleClick(Me, ShipmentArgs)
        End Sub

        Private Sub DbiDayView1_AppointmentRightClick(ByVal sender As Object, ByVal e As Dbi.WinControl.DayView.AppointmentRightClickEventArgs) Handles DbiDayView1.AppointmentRightClick
            Me.dbiDayViewContextMenu.BuildMenu(e.Appointment.Tag)
            Me.dbiDayViewContextMenu.Show(Windows.Forms.Cursor.Position)
        End Sub

        Private Sub DbiDayView1_BeforeAppointmentChange(ByVal sender As Object, ByVal e As Dbi.WinControl.DayView.BeforeAppointmentChangeEventArgs) Handles DbiDayView1.BeforeAppointmentChange
            l_AppointmentMoved = True
        End Sub

        Private Sub DbiDayView1_AppointmentMouseOver(ByVal sender As Object, ByVal e As Dbi.WinControl.DayView.AppointmentMouseOverEventArgs) Handles DbiDayView1.AppointmentMouseOver
            If TipsOnAppointmentOver Then
                If e.Appointment IsNot Nothing Then
                    Dim curTip As Dbi.WinControl.Tips.dbiTipsObject = MakeTipForAppointment(e.Appointment)
                    Me.DbiTipsMain.Show(curTip)
                Else
                    Me.DbiTipsMain.Hide()
                End If
            End If
        End Sub

        Private Sub ButtonPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrintPreview.Click
            Dim newSettings As New System.Drawing.Printing.PrinterSettings
            'Here you can specify any default printer settings before either printing the control directly, or allowing the 
            'user to make some adjustments.
            Dim frm As New dbiWSF.Dialogs.dlgDayViewPrintSettings(Me.DbiDayView1, newSettings)
            frm.ShowDialog()
            If frm.DialogResult = DialogResult.OK Then
                Me.DbiDayView1.PrintPreview(newSettings)
            End If

        End Sub

        Private Sub ButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrint.Click
            Dim newSettings As New System.Drawing.Printing.PrinterSettings
            'Here you can specify any default printer settings before either printing the control directly, or allowing the 
            'user to make some adjustments.
            Dim frm As New dbiWSF.Dialogs.dlgDayViewPrintSettings(Me.DbiDayView1, newSettings)
            frm.ShowDialog()
            If frm.DialogResult = DialogResult.OK Then
                Me.DbiDayView1.Print(newSettings)
            End If
        End Sub

        '''<summary>Timer control used to populate the dbiDayView after a new date has been selected in the dbiDate Control</summary> 
        Private Sub TimerSynchDate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerSynchDate.Tick
            TimerSynchDate.Enabled = False
            If Me.DayViewViewType = ViewType.Warehouse Then
                SetWarehouseDetails(MainWSFObjects, l_ColWarehouses, Me.DbiDate1.Date, l_ColCargoTypes, MainBusinessRules)
            Else
                SetLoadingDockDetails(MainWSFObjects, l_LoadingDock, l_LoadingDockDisplayDates, MainBusinessRules)
            End If
        End Sub
#End Region

#Region "**Events/Methods for the BusinessRules dialog**"
        ''' <summary>Calls the business rules dialog after a shipment has changed.</summary>
        Public Sub evntClShipmentAfterChanged(ByVal sender As Object, ByVal ShipmentMoveArgs As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgsShipmentChange)
            RaiseEvent clShipmentAfterChange(Me, ShipmentMoveArgs)
        End Sub
        ''' <summary>Calls the business rules dialog after a shipment has been added.</summary>
        Public Sub evntClShipmentAfterAdd(ByVal sender As Object, ByVal ShipmentArgs As dbiWSF.ControlLibrary.dbiWSFDayView.DayViewArgs)
            RaiseEvent clShipmentAfterAdd(Me, ShipmentArgs)
        End Sub

        ''' <summary>Supporting Method:Used to call the business rules dialog from an internal timer.</summary>
        ''' <remarks>This is done so that we do not halt internal events from firing within
        ''' the dbiSchedule control which can have undesirable effects.</remarks>
        Private Sub timerEventSynch_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerEventSynch.Tick
            Me.timerEventSynch.Enabled = False
            g_BusinessRulesDialog.StartProcess(g_EventArg, g_EventArgMethod, Me)
        End Sub
#End Region

#Region "**dbiSched Context Menu methods/events**"
        ''' <summary>Refreshes the dbiDayView if a context menu updates any 
        ''' underlying objects in the MainWSFObjects.</summary>
        Private Sub dbiDayViewContextMenu_ContextMenuUpdatedData(ByVal sender As Object, ByVal e As System.EventArgs) Handles dbiDayViewContextMenu.cmUpdatedData
            DataRefresh()
        End Sub
#End Region



    End Class
End Namespace
